package NodeList;

import iList.IList;

/**
 * @Author liangzai
 * @Description:
 */

//单向不带头链表
public class NodeList implements IList {

    public Node head;

    @Override
    public void addFirst(int data) {
        Node newnode = new Node(data);
        newnode.next = head;
        head = newnode;
    }

    @Override
    public void addLast(int data) {
        Node newnode = new Node(data);
        Node pcur = head;
        if(pcur == null){
            addFirst(data);
            return;
        }
        while(pcur.next!=null){
            pcur = pcur.next;
        }
        pcur.next = newnode;
    }

    private void isIndex(int pos){
        if(pos<0 || pos>size()-1){
            throw new PosException("位置不合法！");
        }
    }

    @Override
    public void addIndex(int pos, int data) {
        try {
            isIndex(pos);
            if(pos == 0){
                addFirst(data);
                return ;
            }
            if(pos == size()-1){
                addLast(data);
                return ;
            }
            Node newnode = new Node(data);
            Node pcur = head;
            for (int i = 1; i < pos; i++) {
                pcur = pcur.next;
            }
            Node tmp = pcur.next;
            pcur.next = newnode;
            newnode.next = tmp;
        }catch (PosException e){
            System.out.println("位置不合法！");
            e.printStackTrace();
        }
    }

    @Override
    public boolean contains(int key) {
        Node pcur = head;
        while(pcur!=null){
            if(pcur.val == key){
                return true;
            }
            pcur = pcur.next;
        }
        return false;
    }

    public void isEmpty(Node head){
        if(head == null){
            throw new EmptyException("链表为空！");
        }
    }

    private void removeFrist(){
        head = head.next;
    }

    @Override
    public void remove(int key) {
        try {
            isEmpty(head);
            if (head.val == key) {
                removeFrist();
                return;
            }
            Node pcur = head.next;
            Node per = head;
            while (pcur != null) {
                if (pcur.val == key) {
                    per.next = pcur.next;
                    return;
                }
                pcur = pcur.next;
                per = per.next;
            }
            System.out.println("没有此数据！");
        }catch (EmptyException e){
            System.out.println("链表为空！");
            e.printStackTrace();
        }
    }

    @Override
    public void removeAllKey(int key) {

//        if(head == null) {
//            return;
//        }
//        Node prev = head;
//        Node cur = head.next;
//        while (cur != null) {
//            if(cur.val == key) {
//                prev.next = cur.next;
//                cur = cur.next;
//            }else {
//                prev = cur;
//                cur = cur.next;
//            }
//        }
//        if(head.val == key) {
//            head = head.next;
//        }
//        if(head == null){
//            return ;
//        }

        Node newhead = new Node(0);
        newhead.next = head;
        Node pcur = newhead;
        Node per = head;
        while(pcur.next!=null){
            if(head.val == key){
                head = head.next;
            }
            do{
                pcur = pcur.next;
            }while(pcur!=null && pcur.val == key);
//            if(pcur==null && pcur.val == key){
//                pcur = pcur.next;
//                continue;
//            }
            per.next = pcur;
            per = pcur;
            if(per == null){
                return ;
            }
        }
    }

    @Override
    public int size() {
        Node pcur = head;
        int size = 0;
        while(pcur!=null){
            size++;
            pcur=pcur.next;
        }
        return size;
    }

    @Override
    public void clear() {
        Node pcur = head;
        while(pcur!=null){
            Node nextpcur = pcur.next;
            pcur = null;
            pcur = nextpcur;
        }
        head = null;
    }

    @Override
    public void display() {
        Node pcur = head;
        while(pcur!=null){
            System.out.print(pcur.val+" ");
            pcur=pcur.next;
        }
        System.out.println();
    }
}


